/*              各界面字体显示              */

#include "font.h"
#include "fonts.h"

// 定义全局变量
volatile bool time_thread_running = true;
struct LcdDevice *init_lcd(const char *device)
{
    // 申请空间
    struct LcdDevice *lcd = malloc(sizeof(struct LcdDevice));
    if (lcd == NULL)
    {
        return NULL;
    }

    // 1打开设备
    lcd->fd = open(device, O_RDWR);
    if (lcd->fd < 0)
    {
        perror("open lcd fail");
        free(lcd);
        return NULL;
    }

    // 映射
    lcd->mp = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd->fd, 0);

    return lcd;
}

// 登录界面字体显示
int font_lcd()
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 40);

    char buf1[] = "用户登录";

    // 创建输出框
    bitmap *bm1 = createBitmapWithInit(300, 220, 4, getColor(0, 192, 128, 0));

    // 将字体写入输出框
    fontPrint(f, bm1, 80, 0, buf1, getColor(0, 255, 255, 255), 0);

    // 将输出框打印至LCD
    show_font_to_lcd(lcd->mp, 250, 100, bm1);

    fontSetSize(f, 30);

    char buf2[] = "账号 : ";
    bitmap *bm2 = createBitmapWithInit(270, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm2, 8, 4, buf2, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 260, 160, bm2);

    char buf3[] = "密码 : ";
    bitmap *bm3 = createBitmapWithInit(270, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm3, 8, 4, buf3, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 260, 200, bm3);

    fontSetSize(f, 20);

    char buf4[] = "注册";
    bitmap *bm4 = createBitmapWithInit(60, 60, 4, getColor(255, 255, 255, 255)); // ABGR

    fontPrint(f, bm4, 13, 40, buf4, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 300, 250, bm4);

    char buf5[] = "登录";
    bitmap *bm5 = createBitmapWithInit(60, 60, 4, getColor(255, 255, 255, 255));

    fontPrint(f, bm5, 13, 40, buf5, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 430, 250, bm5);

    // 关闭字体，关闭画板
    fontUnload(f);
    destroyBitmap(bm1);
    destroyBitmap(bm2);
    destroyBitmap(bm3);
    destroyBitmap(bm4);
    destroyBitmap(bm5);

    return 0;
}

// 注册界面字体显示
int register_lcd()
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 40);

    char buf1[] = "用户注册";

    // 创建输出框
    bitmap *bm1 = createBitmapWithInit(300, 220, 4, getColor(0, 192, 128, 0));

    // 将字体写入输出框
    fontPrint(f, bm1, 80, 0, buf1, getColor(0, 255, 255, 255), 0);

    // 将输出框打印至LCD
    show_font_to_lcd(lcd->mp, 250, 100, bm1);

    fontSetSize(f, 30);

    char buf2[] = "账号 : ";
    bitmap *bm2 = createBitmapWithInit(270, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm2, 8, 4, buf2, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 260, 160, bm2);

    char buf3[] = "密码 : ";
    bitmap *bm3 = createBitmapWithInit(270, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm3, 8, 4, buf3, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 260, 200, bm3);

    fontSetSize(f, 50);

    char buf4[] = "注册";
    bitmap *bm4 = createBitmapWithInit(180, 60, 4, getColor(255, 255, 255, 255)); // ABGR

    fontPrint(f, bm4, 50, 8, buf4, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 300, 250, bm4);

    // 关闭字体，关闭画板
    fontUnload(f);
    destroyBitmap(bm1);
    destroyBitmap(bm2);
    destroyBitmap(bm3);
    destroyBitmap(bm4);

    return 0;
}

// 账号输入字体显示
int account_lcd(char *str)
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 30);

    char buf[10] = {0};

    sprintf(buf, "%s", str);
    bitmap *bm = createBitmapWithInit(200, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm, 20, 4, buf, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 330, 160, bm);

    // 关闭字体，关闭画板
    fontUnload(f);
    destroyBitmap(bm);

    return 0;
}

// 密码输入字体显示
int password_lcd(char *str)
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 30);

    // 计算密码长度
    int len = strlen(str);

    // 为密码长度加上一个结束符的空间
    char buf[len + 1];

    // 将字符串中的每个字符替换为星号
    for (int i = 0; i < len; ++i)
    {
        buf[i] = '*';
    }
    buf[len] = '\0';
    bitmap *bm = createBitmapWithInit(200, 35, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm, 20, 4, buf, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 330, 200, bm);

    // 关闭字体，关闭画板
    fontUnload(f);
    destroyBitmap(bm);

    return 0;
}

// 主界面字体显示
int borad_lcd()
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 30);

    char buf[20] = "电子画板";

    bitmap *bm = createBitmapWithInit(100, 30, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm, 4, 0, buf, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 200, 185, bm);

    char buf2[20] = "图库";

    bitmap *bm2 = createBitmapWithInit(50, 30, 4, getColor(0, 255, 255, 255));

    fontPrint(f, bm2, 4, 0, buf2, getColor(0, 0, 0, 0), 0);

    show_font_to_lcd(lcd->mp, 445, 185, bm2);

    // 关闭字体，关闭画板
    fontUnload(f);
    destroyBitmap(bm);
    destroyBitmap(bm2);
    return 0;
}

// 主界面时间显示
void *show_time(void *arg)
{
    // 初始化Lcd
    struct LcdDevice *lcd = init_lcd("/dev/fb0");

    // 打开字体
    font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");

    // 字体大小设置
    fontSetSize(f, 25);

    extern volatile bool time_thread_running;

    while (time_thread_running)
    {
        bitmap *bm = createBitmapWithInit(180, 30, 4, getColor(255, 255, 255, 255));

        // 获取时间数据
        time_t ret = time(NULL);
        struct tm *localresult = localtime(&ret);

        char buf[128] = {0};
        // 计算当前时间并拼接字符串
        sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d", localresult->tm_year + 1900, localresult->tm_mon + 1, localresult->tm_mday, localresult->tm_hour, localresult->tm_min, localresult->tm_sec);

        fontPrint(f, bm, 3, 0, buf, getColor(0, 0, 0, 0), 0);

        show_font_to_lcd(lcd->mp, 620, 0, bm);

        // 关闭画板
        destroyBitmap(bm);

        sleep(1);
    }

    return NULL;
}